/* Copyright (c) 1993-2008 by Richard Kelsey and Jonathan Rees.
   See file COPYING. */

#if defined HAVE_STDINT_H
#include <stdint.h> /* uintXX_t, C99 */
#endif

#if defined HAVE_SYS_TYPES_H
#include <sys/types.h> /* size_t */
#endif

typedef long	s48_value;

#define NO_ERRORS 0    /* errno value */

#if SIZEOF_VOID_P == 4
#define S48_MAX_FIXNUM_VALUE ((1 << 29) - 1)
#define S48_MIN_FIXNUM_VALUE (-1 << 29)
#define S48_LOG_BYTES_PER_CELL 2
#elif SIZEOF_VOID_P == 8
#define S48_MAX_FIXNUM_VALUE ((1L << 61) - 1)
#define S48_MIN_FIXNUM_VALUE (-1L << 61)
#define S48_LOG_BYTES_PER_CELL 3
#else
#error "What size are your pointers, really?"
#endif


/* New FFI */

typedef struct s48_ref_s  *s48_ref_t;
typedef struct s48_call_s *s48_call_t;

/* local refs */
S48_EXTERN s48_ref_t  s48_make_local_ref(s48_call_t call, s48_value obj);
S48_EXTERN s48_ref_t  s48_copy_local_ref(s48_call_t call, s48_ref_t ref);
S48_EXTERN void       s48_free_local_ref(s48_call_t call, s48_ref_t ref);
S48_EXTERN void       s48_free_local_ref_array(s48_call_t call, s48_ref_t *refs, size_t len);

/* global refs */
S48_EXTERN s48_ref_t  s48_make_global_ref(s48_value obj);
S48_EXTERN void       s48_free_global_ref(s48_ref_t ref);
S48_EXTERN s48_ref_t  s48_local_to_global_ref(s48_ref_t ref);

/* local bufs */
S48_EXTERN void *s48_make_local_buf (s48_call_t call, size_t s);
S48_EXTERN void s48_free_local_buf (s48_call_t call, void *buffer);

/* subcalls */
S48_EXTERN s48_call_t s48_make_subcall(s48_call_t call);
S48_EXTERN void       s48_free_subcall(s48_call_t subcall);
S48_EXTERN s48_ref_t  s48_finish_subcall(s48_call_t call, s48_call_t subcall, s48_ref_t ref);

/* immediate refs */
S48_EXTERN s48_ref_t  s48_get_immediate_ref(long immediate_index);

/* external code should not use this, but might need to... */
S48_EXTERN void       s48_setref(s48_ref_t ref, s48_value obj);
S48_EXTERN s48_value  s48_deref(s48_ref_t ref);


/* Misc stuff */

#define s48_eq_p_2(c, r1, r2) (s48_deref(r1) == s48_deref(r2))
/* Superceded name for the above definition, retained for compatibility. */
#define s48_eq_2(c, r1, r2) (c, s48_deref(r1) == s48_deref(r2)) 

S48_EXTERN int		s48_stob_has_type_2(s48_call_t, s48_ref_t, int);
S48_EXTERN long		s48_stob_length_2(s48_call_t, s48_ref_t, int);
S48_EXTERN long		s48_stob_byte_length_2(s48_call_t, s48_ref_t, int);
S48_EXTERN s48_ref_t	s48_stob_ref_2(s48_call_t, s48_ref_t, int, long);
S48_EXTERN void		s48_stob_set_2(s48_call_t, s48_ref_t, int, long, s48_ref_t);
S48_EXTERN char		s48_stob_byte_ref_2(s48_call_t, s48_ref_t, int, long);
S48_EXTERN void		s48_stob_byte_set_2(s48_call_t, s48_ref_t, int, long, char);

S48_EXTERN s48_ref_t	s48_make_string_2(s48_call_t, int, long);
S48_EXTERN void		s48_string_set_2(s48_call_t, s48_ref_t s, long i, long c);
S48_EXTERN long		s48_string_ref_2(s48_call_t, s48_ref_t s, long i);
S48_EXTERN long		s48_string_length_2(s48_call_t, s48_ref_t s);
S48_EXTERN s48_ref_t	s48_enter_string_latin_1_2(s48_call_t, const char* s);
S48_EXTERN s48_ref_t	s48_enter_string_latin_1_n_2(s48_call_t, const char* s, long count);
S48_EXTERN long		s48_string_latin_1_length_2(s48_call_t, s48_ref_t s);
S48_EXTERN long		s48_string_latin_1_length_n_2(s48_call_t, s48_ref_t s, long start, long count);
S48_EXTERN void		s48_copy_latin_1_to_string_2(s48_call_t, const char* s, s48_ref_t sch_s);
S48_EXTERN void		s48_copy_latin_1_to_string_n_2(s48_call_t, const char* s, long len, s48_ref_t sch_s);
S48_EXTERN void		s48_copy_string_to_latin_1_2(s48_call_t, s48_ref_t sch_s, char* s);
S48_EXTERN void		s48_copy_string_to_latin_1_n_2(s48_call_t, s48_ref_t sch_s, long start, long count, char* s);
S48_EXTERN char	*	s48_extract_latin_1_from_string_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_enter_string_utf_8_2(s48_call_t, const char* s);
S48_EXTERN s48_ref_t	s48_enter_string_utf_8_n_2(s48_call_t, const char* s, long count);
S48_EXTERN long		s48_string_utf_8_length_2(s48_call_t, s48_ref_t s);
S48_EXTERN long		s48_string_utf_8_length_n_2(s48_call_t, s48_ref_t s, long start, long count);
S48_EXTERN long		s48_copy_string_to_utf_8_2(s48_call_t, s48_ref_t sch_s, char* s);
S48_EXTERN long		s48_copy_string_to_utf_8_n_2(s48_call_t, s48_ref_t sch_s, long start, long count, char* s);
S48_EXTERN char	*	s48_extract_utf_8_from_string_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_enter_string_utf_16be_2(s48_call_t, const uint16_t *);
S48_EXTERN s48_ref_t	s48_enter_string_utf_16be_n_2(s48_call_t, const uint16_t *, long);
S48_EXTERN long		s48_string_utf_16be_length_2(s48_call_t, s48_ref_t);
S48_EXTERN long		s48_string_utf_16be_length_n_2(s48_call_t, s48_ref_t, long, long);
S48_EXTERN uint16_t *	s48_extract_utf_16be_from_string_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_enter_string_utf_16le_2(s48_call_t, const uint16_t *);
S48_EXTERN long		s48_copy_string_to_utf_16be_2(s48_call_t, s48_ref_t, uint16_t *);
S48_EXTERN long		s48_copy_string_to_utf_16be_n_2(s48_call_t, s48_ref_t, long, long, uint16_t *);
S48_EXTERN s48_ref_t	s48_enter_string_utf_16le_n_2(s48_call_t, const uint16_t *, long);
S48_EXTERN long		s48_string_utf_16le_length_2(s48_call_t, s48_ref_t);
S48_EXTERN long		s48_string_utf_16le_length_n_2(s48_call_t, s48_ref_t, long, long);
S48_EXTERN long		s48_copy_string_to_utf_16le_2(s48_call_t, s48_ref_t, uint16_t *);
S48_EXTERN long		s48_copy_string_to_utf_16le_n_2(s48_call_t, s48_ref_t, long, long, uint16_t *);
S48_EXTERN uint16_t *	s48_extract_utf_16le_from_string_2(s48_call_t, s48_ref_t);

S48_EXTERN s48_ref_t	s48_enter_char_2(s48_call_t, long);
S48_EXTERN long 	s48_extract_char_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_enter_long_as_fixnum_2(s48_call_t, long);
S48_EXTERN s48_ref_t	s48_enter_long_2(s48_call_t, long);
S48_EXTERN long		s48_extract_long_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_enter_unsigned_long_2(s48_call_t, unsigned long);
S48_EXTERN unsigned long s48_extract_unsigned_long_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_enter_double_2(s48_call_t, double);
S48_EXTERN double	s48_extract_double_2(s48_call_t, s48_ref_t);

S48_EXTERN s48_ref_t	s48_cons_2(s48_call_t, s48_ref_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_enter_byte_vector_2(s48_call_t, const char *, long);
S48_EXTERN s48_ref_t	s48_enter_unmovable_byte_vector_2(s48_call_t, const char *, long);
S48_EXTERN char *	s48_extract_byte_vector_2(s48_call_t, s48_ref_t);
S48_EXTERN char *	s48_extract_byte_vector_readonly_2(s48_call_t, s48_ref_t);
S48_EXTERN char *       s48_extract_unmovable_byte_vector_2(s48_call_t, s48_ref_t);
S48_EXTERN void		s48_extract_byte_vector_region_2(s48_call_t, s48_ref_t, long, long, char*);
S48_EXTERN void		s48_enter_byte_vector_region_2(s48_call_t, s48_ref_t, long, long, char*);
S48_EXTERN char *	s48_extract_byte_vector_unmanaged_2(s48_call_t, s48_ref_t);
S48_EXTERN void 	s48_release_byte_vector_2(s48_call_t, s48_ref_t, char*);
S48_EXTERN void         s48_copy_from_byte_vector_2(s48_call_t, s48_ref_t, char *);
S48_EXTERN void         s48_copy_to_byte_vector_2(s48_call_t, s48_ref_t, char *);
S48_EXTERN s48_ref_t	s48_make_vector_2(s48_call_t, long, s48_ref_t);
S48_EXTERN s48_ref_t	s48_make_byte_vector_2(s48_call_t, long);
S48_EXTERN s48_ref_t	s48_make_unmovable_byte_vector_2(s48_call_t, long);
S48_EXTERN s48_ref_t	s48_enter_byte_string_2(s48_call_t, const char *);
S48_EXTERN s48_ref_t	s48_enter_byte_substring_2(s48_call_t, const char *, long);
S48_EXTERN s48_ref_t	s48_make_record_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_make_weak_pointer_2(s48_call_t, s48_ref_t);
S48_EXTERN void		s48_check_record_type_2(s48_call_t, s48_ref_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_length_2(s48_call_t, s48_ref_t);

S48_EXTERN s48_ref_t	s48_enter_pointer_2(s48_call_t, void *);
S48_EXTERN void*	s48_extract_pointer_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_get_imported_binding_2(char *);
S48_EXTERN s48_ref_t	s48_get_imported_binding_local_2(s48_call_t, char *);
S48_EXTERN s48_ref_t	s48_define_exported_binding_2(s48_call_t, char *, s48_ref_t);

S48_EXTERN s48_ref_t	s48_set_channel_os_index_2(s48_call_t, s48_ref_t, long);
S48_EXTERN s48_ref_t	s48_add_channel_2(s48_call_t, s48_ref_t, s48_ref_t, long);
S48_EXTERN void		s48_close_channel_2(s48_call_t, long);

S48_EXTERN void		s48_check_enum_set_type_2(s48_call_t, s48_ref_t, s48_ref_t);
S48_EXTERN long		s48_enum_set2integer_2(s48_call_t, s48_ref_t);
S48_EXTERN s48_ref_t	s48_integer2enum_set_2(s48_call_t, s48_ref_t, long);

S48_EXTERN s48_ref_t 	s48_call_scheme_2(s48_call_t call, s48_ref_t proc, long nargs, ...);

S48_EXTERN s48_ref_t s48_get_current_time(s48_call_t call);
S48_EXTERN s48_ref_t s48_get_timezone(s48_call_t call);

#define s48_make_value_2(c, type) (s48_make_byte_vector_2(c, sizeof(type)))
#define s48_make_sized_value_2(c, size) (s48_make_byte_vector_2(c, size))
S48_EXTERN void *	s48_value_pointer_2(s48_call_t, s48_ref_t);

#define s48_extract_value_pointer_2(c, x, type) ((type *) s48_value_pointer_2(c, x))
#define s48_extract_value_2(c, x, type) (*(s48_extract_value_pointer_2(c, (x), type)))
#define s48_value_size_2(c, x) (s48_byte_vector_length_2(c, x))
#define s48_set_value_2(c, x, type, v) (s48_extract_value_2(c, (x), type) = (v))

#define s48_unsafe_extract_value_pointer_2(c, x, type)		\
  (s48_address_after_header_2(c, (x), type))
#define s48_unsafe_extract_value_2(c, x, type)			\
  (*(s48_unsafe_extract_value_pointer_2(c, (x), type)))
#define s48_unsafe_set_value_2(c, x, type, v)			\
  (s48_unsafe_extract_value_2(c, (x), type) = (v))

#define s48_unsafe_extract_double_2(c, x)			\
  (*(s48_address_after_header_2(c, (x), double)))

#define s48_arg_ref_2(c, argv, index, argc) ((argv)[(argc)-(index)-1])

/* Exceptions */

S48_EXTERN void s48_error_2(s48_call_t call, const char* who, const char* message,
			    long irritant_count, ...);
S48_EXTERN void s48_assertion_violation_2(s48_call_t call, const char* who, const char* message,
					  long irritant_count, ...);
S48_EXTERN void s48_os_error_2(s48_call_t call, const char* who, int the_errno,
			       long irritant_count, ...);
S48_EXTERN void s48_out_of_memory_error_2(s48_call_t call);

/* Internal use */

S48_EXTERN void s48_raise_scheme_exception_2(s48_call_t call, long type, long nargs, ...);

/* Type checking */

#define s48_check_pair_2(c, v) do { if (!s48_pair_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a pair", 1, v); } while (0)
#define s48_check_fixnum_2(c, v) do { if (!s48_fixnum_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a fixnum", 1, v); } while (0)
#define s48_check_string_2(c, v) do { if (!s48_string_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a string", 1, v); } while (0)
#define s48_check_byte_vector_2(c, v) do { if (!s48_byte_vector_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a bytevector", 1, v); } while (0)
#define s48_check_channel_2(c, v) do { if (!s48_channel_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a channel", 1, v); } while (0)
#define s48_check_record_2(c, v) do { if (!s48_record_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be a record", 1, v); } while (0)
#define s48_check_value_2(c, v) do { if (!s48_byte_vector_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be an external value", 1, v); } while (0)
#define s48_check_export_binding_2(c, v) do { if (!s48_export_binding_p_2(c, v)) s48_assertion_violation_2(c, NULL, "must be an exported value", 1, v ); } while (0)
#define s48_check_boolean_2(c, r) \
  do { if (!s48_false_p_2(c, r) && !s48_true_p_2(c, r))	\
	 s48_assertion_violation_2(c, NULL, "must be a boolean", 1, v); } while (0)

#define s48_value_p_2(c, v) (s48_byte_vector_p_2(c, v))

#define s48_true_p_2(c, r) (s48_deref(r) == _s48_value_true)
#define s48_false_p_2(c, r) (s48_deref(r) == _s48_value_false)
#define s48_null_p_2(c, r) (s48_deref(r) == _s48_value_null)
#define s48_extract_boolean_2(c, r) (!(s48_deref(r) == _s48_value_false))
#define s48_enter_boolean_2(c, v) ((v) ? s48_true_2(c) : s48_false_2(c))

#define s48_shared_binding_check_2(c, binding) \
  do { if (s48_deref(s48_shared_binding_ref_2(c, binding)) == _s48_value_unspecific) \
      s48_raise_scheme_exception_2(c, s48_exception_unbound_external_name, 1, \
				   s48_shared_binding_name_2(c, binding)); \
  } while(0)



#ifndef NO_OLD_FFI

/* Misc stuff */

#define S48_EQ_P(v1, v2) ((v1) == (v2))
/* Superceded name for the above definition, retained for compatibility. */
#define S48_EQ(v1, v2) ((v1) == (v2)) 

S48_EXTERN int		s48_stob_has_type(s48_value, int);
S48_EXTERN long		s48_stob_length(s48_value, int);
S48_EXTERN long		s48_stob_byte_length(s48_value, int);
S48_EXTERN s48_value	s48_stob_ref(s48_value, int, long);
S48_EXTERN void		s48_stob_set(s48_value, int, long, s48_value);
S48_EXTERN char		s48_stob_byte_ref(s48_value, int, long);
S48_EXTERN void		s48_stob_byte_set(s48_value, int, long, char);

S48_EXTERN char *	s48_register_gc_rootB(char *);
S48_EXTERN void		s48_unregister_gc_rootB(char *);
S48_EXTERN void		s48_push_gc_rootsB(char *, long);
S48_EXTERN char		s48_pop_gc_rootsB(void);

S48_EXTERN s48_value	s48_make_string(int, long);
S48_EXTERN void		s48_string_set(s48_value s, long i, long c);
S48_EXTERN long		s48_string_ref(s48_value s, long i);
S48_EXTERN long		s48_string_length(s48_value s);
S48_EXTERN s48_value	s48_enter_string_latin_1(char* s);
S48_EXTERN s48_value	s48_enter_string_latin_1_n(char* s, long count);
S48_EXTERN void		s48_copy_latin_1_to_string(char* s, s48_value sch_s);
S48_EXTERN void		s48_copy_latin_1_to_string_n(char* s, long len, s48_value sch_s);
S48_EXTERN void		s48_copy_string_to_latin_1(s48_value sch_s, char* s);
S48_EXTERN void		s48_copy_string_to_latin_1_n(s48_value sch_s, long start, long count, char* s);
S48_EXTERN s48_value	s48_enter_string_utf_8(char* s);
S48_EXTERN s48_value	s48_enter_string_utf_8_n(char* s, long count);
S48_EXTERN long		s48_string_utf_8_length(s48_value s);
S48_EXTERN long		s48_string_utf_8_length_n(s48_value s, long start, long count);
S48_EXTERN long		s48_copy_string_to_utf_8(s48_value sch_s, char* s);
S48_EXTERN long		s48_copy_string_to_utf_8_n(s48_value sch_s, long start, long count, char* s);
S48_EXTERN s48_value	s48_enter_string_utf_16be(const uint16_t *);
S48_EXTERN s48_value	s48_enter_string_utf_16be_n(const uint16_t *, long);
S48_EXTERN long		s48_string_utf_16be_length(s48_value);
S48_EXTERN long		s48_string_utf_16be_length_n(s48_value, long, long);
S48_EXTERN long		s48_copy_string_to_utf_16be(s48_value, uint16_t *);
S48_EXTERN long		s48_copy_string_to_utf_16be_n(s48_value, long, long, uint16_t *);
S48_EXTERN s48_value	s48_enter_string_utf_16le(const uint16_t *);
S48_EXTERN s48_value	s48_enter_string_utf_16le_n(const uint16_t *, long);
S48_EXTERN long		s48_string_utf_16le_length(s48_value);
S48_EXTERN long		s48_string_utf_16le_length_n(s48_value, long, long);
S48_EXTERN long		s48_copy_string_to_utf_16le(s48_value, uint16_t *);
S48_EXTERN long		s48_copy_string_to_utf_16le_n(s48_value, long, long, uint16_t *);

S48_EXTERN s48_value	s48_enter_char(long);
S48_EXTERN long 	s48_extract_char(s48_value);
S48_EXTERN s48_value	s48_enter_fixnum(long);
S48_EXTERN long		s48_extract_fixnum(s48_value);
S48_EXTERN s48_value	s48_enter_integer(long);
S48_EXTERN long		s48_extract_integer(s48_value);
S48_EXTERN s48_value	s48_enter_unsigned_integer(unsigned long);
S48_EXTERN unsigned long s48_extract_unsigned_integer(s48_value);
S48_EXTERN s48_value	s48_enter_double(double);
S48_EXTERN double	s48_extract_double(s48_value);

S48_EXTERN s48_value	s48_cons(s48_value, s48_value);
S48_EXTERN s48_value	s48_enter_byte_vector(char *, long);
S48_EXTERN s48_value	s48_enter_unmovable_byte_vector(char *, long);
S48_EXTERN char *	s48_extract_byte_vector(s48_value);
S48_EXTERN s48_value	s48_make_vector(long, s48_value);
S48_EXTERN s48_value	s48_make_byte_vector(long);
S48_EXTERN s48_value	s48_make_unmovable_byte_vector(long);
S48_EXTERN s48_value	s48_enter_byte_string(char *);
S48_EXTERN s48_value	s48_enter_byte_substring(char *, long);
S48_EXTERN s48_value	s48_make_record(s48_value);
S48_EXTERN s48_value	s48_make_weak_pointer(s48_value);
S48_EXTERN void		s48_check_record_type(s48_value, s48_value);
S48_EXTERN s48_value	s48_length(s48_value);
S48_EXTERN void*	s48_extract_pointer(s48_value);
S48_EXTERN s48_value	s48_get_imported_binding(char *);

S48_EXTERN s48_value	s48_set_channel_os_index(s48_value, long);
S48_EXTERN s48_value	s48_add_channel(s48_value, s48_value, long);
S48_EXTERN void		s48_close_channel(long);

S48_EXTERN void		s48_check_enum_set_type(s48_value, s48_value);
S48_EXTERN long		s48_enum_set2integer(s48_value);
S48_EXTERN s48_value	s48_integer2enum_set(s48_value, long);

S48_EXTERN s48_value	s48_call_scheme(s48_value proc, long nargs, ...);

#define S48_MAKE_VALUE(type) (s48_make_byte_vector(sizeof(type)))
#define S48_MAKE_SIZED_VALUE(size) (s48_make_byte_vector(size))
S48_EXTERN void *	s48_value_pointer(s48_value);

#define S48_EXTRACT_VALUE_POINTER(x, type) ((type *) s48_value_pointer(x))
#define S48_EXTRACT_VALUE(x, type) (*(S48_EXTRACT_VALUE_POINTER((x), type)))
#define S48_VALUE_SIZE(x) (S48_BYTE_VECTOR_LENGTH(x))
#define S48_SET_VALUE(x, type, v) (S48_EXTRACT_VALUE((x), type) = (v))

#define S48_UNSAFE_EXTRACT_VALUE_POINTER(x, type)		\
  (S48_ADDRESS_AFTER_HEADER((x), type))
#define S48_UNSAFE_EXTRACT_VALUE(x, type)			\
  (*(S48_UNSAFE_EXTRACT_VALUE_POINTER((x), type)))
#define S48_UNSAFE_SET_VALUE(x, type, v)			\
  (S48_UNSAFE_EXTRACT_VALUE((x), type) = (v))

#define S48_UNSAFE_EXTRACT_DOUBLE(x)				\
  (*(S48_ADDRESS_AFTER_HEADER((x), double)))

#define S48_ARG_REF(argv, index, argc) ((argv)[(argc)-(index)-1])

#define S48_DECLARE_GC_PROTECT(n) long ___gc_buffer[(n)+2]

#define S48_GC_PROTECT_1(v) \
  (___gc_buffer[2]=(long)&(v), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 1))

#define S48_GC_PROTECT_2(v1, v2) \
  (___gc_buffer[2]=(long)&(v1), ___gc_buffer[3]=(long)&(v2), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 2))

#define S48_GC_PROTECT_3(v1, v2, v3) \
  (___gc_buffer[2]=(long)&(v1), \
   ___gc_buffer[3]=(long)&(v2), \
   ___gc_buffer[4]=(long)&(v3), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 3))

#define S48_GC_PROTECT_4(v1, v2, v3, v4) \
  (___gc_buffer[2]=(long)&(v1), \
   ___gc_buffer[3]=(long)&(v2), \
   ___gc_buffer[4]=(long)&(v3), \
   ___gc_buffer[5]=(long)&(v4), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 4))

#define S48_GC_PROTECT_5(v1, v2, v3, v4, v5) \
  (___gc_buffer[2]=(long)&(v1), \
   ___gc_buffer[3]=(long)&(v2), \
   ___gc_buffer[4]=(long)&(v3), \
   ___gc_buffer[5]=(long)&(v4), \
   ___gc_buffer[6]=(long)&(v5), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 5))

#define S48_GC_PROTECT_6(v1, v2, v3, v4, v5, v6) \
  (___gc_buffer[2]=(long)&(v1), \
   ___gc_buffer[3]=(long)&(v2), \
   ___gc_buffer[4]=(long)&(v3), \
   ___gc_buffer[5]=(long)&(v4), \
   ___gc_buffer[6]=(long)&(v5), \
   ___gc_buffer[7]=(long)&(v6), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 6))

#define S48_GC_PROTECT_7(v1, v2, v3, v4, v5, v6, v7) \
  (___gc_buffer[2]=(long)&(v1), \
   ___gc_buffer[3]=(long)&(v2), \
   ___gc_buffer[4]=(long)&(v3), \
   ___gc_buffer[5]=(long)&(v4), \
   ___gc_buffer[6]=(long)&(v5), \
   ___gc_buffer[7]=(long)&(v6), \
   ___gc_buffer[8]=(long)&(v7), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 7))

#define S48_GC_PROTECT_8(v1, v2, v3, v4, v5, v6, v7, v8) \
  (___gc_buffer[2]=(long)&(v1), \
   ___gc_buffer[3]=(long)&(v2), \
   ___gc_buffer[4]=(long)&(v3), \
   ___gc_buffer[5]=(long)&(v4), \
   ___gc_buffer[6]=(long)&(v5), \
   ___gc_buffer[7]=(long)&(v6), \
   ___gc_buffer[8]=(long)&(v7), \
   ___gc_buffer[9]=(long)&(v8), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 8))

#define S48_GC_PROTECT_9(v1, v2, v3, v4, v5, v6, v7, v8, v9) \
  (___gc_buffer[2]=(long)&(v1), \
   ___gc_buffer[3]=(long)&(v2), \
   ___gc_buffer[4]=(long)&(v3), \
   ___gc_buffer[5]=(long)&(v4), \
   ___gc_buffer[6]=(long)&(v5), \
   ___gc_buffer[7]=(long)&(v6), \
   ___gc_buffer[8]=(long)&(v7), \
   ___gc_buffer[9]=(long)&(v8), \
   ___gc_buffer[10]=(long)&(v9), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 9))

#define S48_GC_PROTECT_10(v1, v2, v3, v4, v5, v6, v7, v8, v9, v10) \
  (___gc_buffer[2]=(long)&(v1), \
   ___gc_buffer[3]=(long)&(v2), \
   ___gc_buffer[4]=(long)&(v3), \
   ___gc_buffer[5]=(long)&(v4), \
   ___gc_buffer[6]=(long)&(v5), \
   ___gc_buffer[7]=(long)&(v6), \
   ___gc_buffer[8]=(long)&(v7), \
   ___gc_buffer[9]=(long)&(v8), \
   ___gc_buffer[10]=(long)&(v9), \
   ___gc_buffer[11]=(long)&(v10), \
   s48_push_gc_rootsB((char *) ___gc_buffer, 10))

#define S48_GC_UNPROTECT()				\
   do { if (! s48_pop_gc_rootsB())			\
       	  s48_raise_scheme_exception( S48_EXCEPTION_GC_PROTECTION_MISMATCH, 0); \
      } while(0)

#define S48_GC_PROTECT_GLOBAL(v) ((void*)(s48_register_gc_rootB((char *)&(v))))
#define S48_GC_UNPROTECT_GLOBAL(f) (s48_unregister_gc_rootB((char *)(f)))

/* Exceptions */

S48_EXTERN void s48_error(const char* who, const char* message,
			  long irritant_count, ...);
S48_EXTERN void s48_assertion_violation(const char* who, const char* message,
					long irritant_count, ...);
S48_EXTERN void s48_os_error(const char* who, int the_errno,
			     long irritant_count, ...);
S48_EXTERN void s48_out_of_memory_error();

/* The following are deprecated */

S48_EXTERN void s48_raise_argument_type_error(s48_value value);
S48_EXTERN void s48_raise_argument_number_error(s48_value value,
						s48_value min,
						s48_value max);
S48_EXTERN void s48_raise_range_error(s48_value value,
				      s48_value min,
				      s48_value max);
S48_EXTERN void s48_raise_closed_channel_error();
S48_EXTERN void s48_raise_os_error(int the_errno);
S48_EXTERN void s48_raise_string_os_error(char *reason);
S48_EXTERN void s48_raise_out_of_memory_error();

/* Internal use */

S48_EXTERN void s48_raise_scheme_exception(long type, long nargs, ...);

/* Type checking */

#define S48_CHECK_PAIR(v) do { if (!S48_PAIR_P(v)) s48_assertion_violation(NULL, "must be a pair", 1, v); } while (0)
#define S48_CHECK_FIXNUM(v) do { if (!S48_FIXNUM_P(v)) s48_assertion_violation(NULL, "must be a fixnum", 1, v); } while (0)
#define S48_CHECK_STRING(v) do { if (!S48_STRING_P(v)) s48_assertion_violation(NULL, "must be a string", 1, v); } while (0)
#define S48_CHECK_BYTE_VECTOR(v) do { if (!S48_BYTE_VECTOR_P(v)) s48_assertion_violation(NULL, "must be a bytevector", 1, v); } while (0)
#define S48_CHECK_CHANNEL(v) do { if (!S48_CHANNEL_P(v)) s48_assertion_violation(NULL, "must be a channel", 1, v); } while (0)
#define S48_CHECK_RECORD(v) do { if (!S48_RECORD_P(v)) s48_assertion_violation(NULL, "must be a record", 1, v); } while (0)
#define S48_CHECK_VALUE(v) do { if (!S48_BYTE_VECTOR_P(v)) s48_assertion_violation(NULL, "must be an external value", 1, v); } while (0)
#define S48_CHECK_EXPORT_BINDING(v) do { if (!S48_EXPORT_BINDING_P(v)) s48_assertion_violation(NULL, "must be an exported value", 1, v ); } while (0)
#define S48_CHECK_BOOLEAN(v)					\
  do { s48_value s48_temp = (v);				\
       if (s48_temp != S48_TRUE && s48_temp != S48_FALSE)	\
	 s48_assertion_violation(NULL, "must be a boolean", 1, v); } while (0)

#define S48_VALUE_P(v) (S48_BYTE_VECTOR_P(v))

#define S48_TRUE_P(v) ((v) == S48_TRUE)
#define S48_FALSE_P(v) ((v) == S48_FALSE)
#define S48_NULL_P(v) ((v) == S48_NULL)
#define S48_EXTRACT_BOOLEAN(v) ((v) != S48_FALSE)
#define S48_ENTER_BOOLEAN(v) ((v) ? S48_TRUE : S48_FALSE)

#define S48_SHARED_BINDING_CHECK(binding)					\
  do { if (S48_UNSPECIFIC == S48_SHARED_BINDING_REF(binding))			\
         s48_raise_scheme_exception(S48_EXCEPTION_UNBOUND_EXTERNAL_NAME, 1,	\
				    S48_SHARED_BINDING_NAME(binding));		\
  } while(0)

#endif /* !NO_OLD_FFI */



/* both */

S48_EXTERN s48_value	s48_define_exported_binding(char *, s48_value);
S48_EXTERN s48_value	s48_enter_pointer(void *);

#define S48_EXPORT_FUNCTION(p) (s48_define_exported_binding(#p, s48_enter_pointer((void*) p)))
#define s48_export_function(p) S48_EXPORT_FUNCTION(p)

S48_EXTERN long		s48_external_event_uid(void);
S48_EXTERN long		s48_permanent_external_event_uid(char*);
S48_EXTERN void		s48_note_external_event(long);
S48_EXTERN void		s48_unregister_external_event_uid(long);

